package device_center.device.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import cwb.consts.RequestPrefix;
import cwb.dto.ResultData;
import device_center.device.pojo.EmmDevice;
import device_center.device.service.EmmDeviceServiceImpl;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 设备管理表 前端控制器
 * </p>
 *
 * @author Ezio.Auditore
 * @since 2022-08-22
 */
@RestController
@RequestMapping(RequestPrefix.API + "/device")
public class EmmDeviceController {

    @Autowired
    private EmmDeviceServiceImpl emmDeviceService;

    @GetMapping("/detail")
    @ApiOperation(value = "查看系统设备详情", notes = "查看系统设备详情", response = EmmDevice.class)
    public ResultData detail(Integer deviceId) {
        return ResultData.buildSucess(emmDeviceService.getById(deviceId));
    }

    /**
     * 查看设备列表
     *
     * @return
     */
    @GetMapping("/list")
    @ApiOperation(value = "查看系统设备列表", notes = "查看系统设备列表", response = EmmDevice.class)
    public ResultData pageDevice(@RequestParam Integer pageSize, @RequestParam Integer pageNum, EmmDevice emmDevice) {
        try {
            return ResultData.buildSucess(emmDeviceService.page(new Page(pageNum, pageSize)));
        } catch (Exception e) {
            return ResultData.buildError(e.getMessage());
        }
    }

    /**
     * 新增设备
     *
     * @param emmDevice
     * @return
     */
    @PostMapping("/add")
    @ApiOperation(value = "新增系统设备", notes = "新增系统设备", response = EmmDevice.class)
    public ResultData addDevice(EmmDevice emmDevice) {
        try {
            return ResultData.buildSucess(emmDeviceService.save(emmDevice));
        } catch (Exception e) {
            return ResultData.buildError(e.getMessage());
        }
    }

    /**
     * 修改设备
     *
     * @param emmDevice
     * @return
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改系统设备", notes = "修改系统设备", response = EmmDevice.class)
    public ResultData updateDevice(EmmDevice emmDevice) {
        try {
            return ResultData.buildSucess(emmDeviceService.updateById(emmDevice));
        } catch (Exception e) {
            return ResultData.buildError(e.getMessage());
        }
    }


    /**
     * 删除设备
     *
     * @param emmDevice
     * @return
     */
    @DeleteMapping("/delete")
    @ApiOperation(value = "删除系统设备", notes = "删除系统设备", response = EmmDevice.class)
    public ResultData deleteDevice(EmmDevice emmDevice) {
        try {
            return ResultData.buildSucess(emmDeviceService.removeById(emmDevice));
        } catch (Exception e) {
            return ResultData.buildError(e.getMessage());
        }
    }

}
